package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.SampleReturnRecordAdapetr
import com.ht.medicaltreatment.adapter.SampleReturnTimeOutRecordAdapetr
import com.ht.medicaltreatment.bean.IceboxItemListBean
import com.ht.medicaltreatment.bean.ReturnTimeOutRecordBean
import com.ht.medicaltreatment.view.CentrifugeProjectDialog
import com.ht.medicaltreatment.viewmodel.ReturnRecordViewModel
import com.loper7.date_time_picker.DateTimeConfig
import com.loper7.date_time_picker.dialog.CardDatePickerDialog
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_return_record_layout.btnPositive
import kotlinx.android.synthetic.main.activity_return_record_layout.cbNormal
import kotlinx.android.synthetic.main.activity_return_record_layout.cbOverTime
import kotlinx.android.synthetic.main.activity_return_record_layout.etFormName
import kotlinx.android.synthetic.main.activity_return_record_layout.etProjectName
import kotlinx.android.synthetic.main.activity_return_record_layout.groupNormal
import kotlinx.android.synthetic.main.activity_return_record_layout.imgProject
import kotlinx.android.synthetic.main.activity_return_record_layout.recyclerView
import kotlinx.android.synthetic.main.activity_return_record_layout.tvEndTime
import kotlinx.android.synthetic.main.activity_return_record_layout.tvReturnUser
import kotlinx.android.synthetic.main.activity_return_record_layout.tvStartTime

/**
 * 归还-转入记录
 */
class ReturnRecordActivity:BaseActivity() , View.OnClickListener, OnItemClickListener{
    override fun getLayoutId() = R.layout.activity_return_record_layout

    private val model by viewModels<ReturnRecordViewModel>()
    private val normalAdapter = SampleReturnRecordAdapetr()
    private val timeOutAdapter = SampleReturnTimeOutRecordAdapetr()
    private val data:MutableList<IceboxItemListBean> = mutableListOf()
    private var id = ""

    override fun init() {
        setTitle("存入记录")
        tvStartTime.setOnClickListener(this)
        tvEndTime.setOnClickListener(this)
        cbNormal.setOnClickListener(this)
        cbOverTime.setOnClickListener(this)
        btnPositive.setOnClickListener(this)
        etProjectName.setOnClickListener(this)
        imgProject.setOnClickListener(this)

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = normalAdapter
        normalAdapter.setOnItemClickListener(this)
        timeOutAdapter.setOnItemClickListener(this)

        model.projects {
            data.clear()
            val list = mutableListOf<IceboxItemListBean>()
            it.forEach { bean->
                list.add(IceboxItemListBean(bean.id,bean.protocolNumber))
            }
            data.addAll(list)
        }
    }

    override fun onClick(v: View?) {
        when(v){
            tvStartTime -> {
                CardDatePickerDialog.builder(this)
                    .setTitle("选择开始时间")
                    .showBackNow(false)
                    .showFocusDateInfo(false)
                    .setThemeColor(resources.getColor(R.color.blue))
                    .setBackGroundModel(CardDatePickerDialog.CUBE)
                    .setDisplayType(arrayListOf(
                        DateTimeConfig.YEAR,
                        DateTimeConfig.MONTH,
                        DateTimeConfig.DAY,
                        DateTimeConfig.HOUR,DateTimeConfig.MIN,DateTimeConfig.SECOND))
                    .setOnChoose {millisecond->
                        tvStartTime.text = TimeUtils.millis2String(millisecond,"yyyy-MM-dd HH:mm:ss")
                    }.build().show()
            }

            tvEndTime -> {
                CardDatePickerDialog.builder(this)
                    .setTitle("选择结束时间")
                    .showBackNow(false)
                    .showFocusDateInfo(false)
                    .setThemeColor(resources.getColor(R.color.blue))
                    .setBackGroundModel(CardDatePickerDialog.CUBE)
                    .setDisplayType(arrayListOf(
                        DateTimeConfig.YEAR,
                        DateTimeConfig.MONTH,
                        DateTimeConfig.DAY,
                        DateTimeConfig.HOUR,DateTimeConfig.MIN,DateTimeConfig.SECOND))
                    .setOnChoose {millisecond->
                        tvEndTime.text = TimeUtils.millis2String(millisecond,"yyyy-MM-dd HH:mm:ss")

                    }.build().show()
            }

            cbOverTime -> {
                cbNormal.isChecked = false
                cbOverTime.isChecked = true
                groupNormal.visibility = View.GONE
                recyclerView.adapter = timeOutAdapter
            }
            cbNormal -> {
                cbOverTime.isChecked = false
                cbNormal.isChecked = true
                groupNormal.visibility = View.VISIBLE
                recyclerView.adapter = normalAdapter
            }

            //一键查询
            btnPositive -> {
                val formName = etFormName.text.toString()
                val tvStartTime = tvStartTime.text.toString()
                val tvEndTime = tvEndTime.text.toString()
                val tvReturnUser = tvReturnUser.text.toString()
                if(id.isEmpty()){
                    showToast("请选择项目")
                    return
                }
                showProgress()
                if(cbNormal.isChecked){
                    model.queryReturnOrder(
                        projectId = id,
                        returnTableName = formName,
                        returnTimeStart = tvStartTime,
                        returnTimeEnd = tvEndTime,
                        sampleName = tvReturnUser){
                        dismissProgress()
                        normalAdapter.setNewInstance(it)
                    }
                }else{
                    model.queryReturnTimeOutOrder(id,{
                        dismissProgress()
                        timeOutAdapter.setNewInstance(it)
                    })
                }
            }

            //选择项目
            etProjectName,
            imgProject -> {
                CentrifugeProjectDialog(this).show(data, { name, id ->
                    etProjectName.text = name
                    this.id = "$id"
                })
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val intent = Intent()
        if(adapter is SampleReturnRecordAdapetr){
            val item = adapter.getItem(position)
            intent.setClass(this,ReturnOrderDetailActivity::class.java)
            intent.putExtra("id",item.id)
            intent.putExtra("projectId",id)
            intent.putExtra("orderName",item.returnTableName)
            intent.putExtra("startTime",tvStartTime.text.toString())
            intent.putExtra("endTime",tvEndTime.text.toString())
            intent.putExtra("returnTime",item.returnTime)
            intent.putExtra("userName",item.returnPerson)
            intent.putExtra("remark",item.remark)
        }else{
            val item = adapter.getItem(position) as ReturnTimeOutRecordBean
            intent.setClass(this,ReturnTimeOutOrderDetailActivity::class.java)
            intent.putExtra("id",id)
            intent.putExtra("value",item.value)
            intent.putExtra("orderName",item.orderName)
            LogUtils.i(id,item.value)
        }
        startActivity(intent)
    }

}
